package cn.dream.ebag.MessengerLib;

import android.content.Context;
import android.content.Intent;

import cn.dream.ebag.MessengerLib.base.MMSBaseImp;
import cn.dream.ebag.MessengerLib.constants.MSAction;
import cn.dream.ebag.MessengerLib.listeners.OnServerListener;
import cn.dream.ebag.MessengerLib.services.MGService;

import static cn.dream.ebag.MessengerLib.constants.MMParam.PARAM_DATA;
import static cn.dream.ebag.MessengerLib.constants.MMParam.PARAM_SEND_ALL;
import static cn.dream.ebag.MessengerLib.constants.MMParam.PARAM_TAG;


/**
 * @ProjectName: MessengerServer
 * @Package: cn.dream.mgserver
 * @Author: qktang
 * @CreateDate: 2021/1/11 16:19
 * @Description: 描述
 */
public abstract class MGServer implements MMSBaseImp, OnServerListener {

    private Context mContext;

    public MGServer(Context context) {
        mContext = context.getApplicationContext();
        Intent intent = new Intent(context, MGService.class);
        mContext.startService(intent);
        MSCallbackMgr.getInstance().addServerListener(this, MGServer.class.getSimpleName());
    }


    public void sendJsonMsg2Client(String jsonMsg, String clientTag) {
        sendMessage2Client(jsonMsg, clientTag, false);
    }

    public void sendJsonMsg2AllClient(String jsonMsg) {
        sendMessage2Client(jsonMsg, "", true);
    }

    public void sendErrorMsg2Client(String errorMsg, String clientTag) {
        sendError2Client(errorMsg, clientTag, false);
    }

    public void sendErrorMsg2AllClient(String errorMsg, String clientTag) {
        sendError2Client(errorMsg, "", true);
    }



    @Override
    public void sendMessage2Client(String jsonMsg, String clientTag, boolean sendAll) {
        Intent intent = new Intent();
        intent.putExtra(PARAM_DATA, jsonMsg);
        intent.putExtra(PARAM_TAG, clientTag);
        intent.putExtra(PARAM_SEND_ALL, sendAll);
        intent.setAction(MSAction.ACTION_SEND_JSON);
        intent.setPackage(mContext.getPackageName());
        mContext.startService(intent);
    }

    @Override
    public void sendError2Client(String error, String clientTag, boolean sendAll) {
        Intent intent = new Intent();
        intent.setAction(MSAction.ACTION_SEND_ERROR);
        intent.putExtra(PARAM_TAG, clientTag);
        intent.putExtra(PARAM_DATA, error);
        intent.putExtra(PARAM_SEND_ALL, sendAll);
        intent.setPackage(mContext.getPackageName());
        mContext.startService(intent);
    }

    public void sendCloseClientMsg(String clientTag) {
        Intent intent = new Intent();
        intent.setAction(MSAction.ACTION_CLOSE_CLIENT);
        intent.putExtra(PARAM_TAG, clientTag);
        intent.setPackage(mContext.getPackageName());
        mContext.startService(intent);
    }

    public void requestClientTagList() {
        Intent intent = new Intent();
        intent.setAction(MSAction.ACTION_REQUEST_CONNECTED_CLIENT);
        intent.setPackage(mContext.getPackageName());
        mContext.startService(intent);
    }

    public void closeConnection() {
        Intent intent = new Intent();
        intent.setAction(MSAction.ACTION_CLOSE_SERVER);
        intent.setPackage(mContext.getPackageName());
        mContext.startService(intent);
    }

}
